#include "SSB_AT.h"
#include "SSB_flash_storage.h"
#include "string.h"
#include "SSB_sys_def.h"
#include "esp_log.h"
#include "opcol_util.h"
#include "SSB_socket.h"
#include <stdio.h>
#include <stdlib.h>

#ifdef PLATFORM_ESP32
#include "esp_vfs.h"

int hand_get_flash_data_call_back(char *data, int len)
{
    if (len > 0)
    {
        int head_len = strlen("+DSGET:");
        int at_len = len + 2 + head_len;
        char *at_cmd = (char *)malloc(at_len);
        memset(at_cmd, '\0', at_len);
        memcpy(at_cmd, "+DSGET:", head_len);
        memcpy(at_cmd + head_len, data, len);
        at_cmd[at_len - 2] = '\r';
        at_cmd[at_len - 1] = '\n';
        //ESP_LOGI(SSB_TAG,"=====call back=%s,%d,%d,%d==",at_cmd,len,strlen(at_cmd),at_len);
        if (SSB_socket_write(at_cmd, at_len) < 0)
        {
            free(at_cmd);
            return -1;
        }
        free(at_cmd);
    }
    else if (len == -1)
    {
        char at_cmd[] = "+DSGET:\r\n";
        SSB_socket_write(at_cmd, strlen(at_cmd));
    }

    return 0;
}

int hand_list_file_call_back(struct dirent *ent, int isEnd, int numIndex)
{
    if (isEnd)
    {
        char response[64] = {0};
        sprintf(response, "%s%d\r\n", AT_STORAGE_LIST_FILE_END, numIndex);
        SSB_socket_write(response, strlen(response));
        return 0;
    }
    int buf_len = strlen(ent->d_name);
    char *buf = (char *)malloc(buf_len + 16);
    memset(buf, 0, buf_len);
    sprintf(buf, "%s%s\r\n", AT_STORAGE_LFNAME, ent->d_name);
    if (SSB_socket_write(buf, strlen(buf)) < 0)
    {
        free(buf);
        return -1;
    }
    free(buf);
    return 0;
}
#endif

void at_command_hand(char *at, int source)
{
    if (strstr(at, AT_WECHAT_ID) != NULL)
    {
        at = at + strlen(AT_WECHAT_ID);
        int airkiss_id_len = strlen(at);
        at[airkiss_id_len - 2] = '\0';
        at[airkiss_id_len - 1] = '\0';
        airkiss_id_len = airkiss_id_len - 2;
        SSB_flash_nvs_storage(SSB_AIRKISS_WECHAT_ID_FIELD, at, airkiss_id_len);
        return;
    }
    //int len=strlen(at);
#ifdef PLATFORM_ESP32
    if (strstr(at, AT_STORAGE_PUT) != NULL)
    {
        //保存数据到flash
        //AT+DSPUT=key,mode,data\r\n
        at = at + strlen(AT_STORAGE_PUT);
        int key_end_index = OPCOL_String_index_of(at, strlen(at), ",");
        char key[64] = {0};
        memcpy(&key[0], at, key_end_index);
        at = at + key_end_index + 1;
        char mode[4] = {0};
        key_end_index = OPCOL_String_index_of(at, strlen(at), ",");
        memcpy(&mode[0], at, key_end_index);
        at = at + key_end_index + 1;
        int len = strlen(at);
        at[len - 1] = '\0';
        at[len - 2] = '\0';
        if (strlen(key) < 1 || strlen(mode) < 1 || strlen(at) < 1)
        {
            return;
        }
        SSB_flash_fs_storage(key, at, len - 2, atoi(mode));
        return;
    }

    if (strstr(at, AT_STORAGE_GET) != NULL)
    {
        //从flash读取数据
        //AT+DSGET=mode,key\r\n
        at = at + strlen(AT_STORAGE_GET);
        int f_end_index = OPCOL_String_index_of(at, strlen(at), ",");
        char mode[4] = {0};
        memcpy(&mode[0], at, f_end_index);
        at = at + f_end_index + 1;
        int len = strlen(at);
        at[len - 1] = '\0';
        at[len - 2] = '\0';
        if (source == 1)
        {
            //socket
            SSB_flash_fs_cb_read(at, 512, atoi(mode), hand_get_flash_data_call_back);
        }
        else
        {
            SSB_flash_fs_cb_read(at, 512, atoi(mode), NULL);
        }
        return;
    }

    if (strstr(at, AT_STORAGE_RENAME_FILE) != NULL)
    {
        //AT+FRENAME=src_file,desc_file\r\n
        at = at + strlen(AT_STORAGE_RENAME_FILE);
        int src_end_index = OPCOL_String_index_of(at, strlen(at), ",");
        if (src_end_index < 0)
        {
            return;
        }
        char src_file[64] = {0};
        memcpy(&src_file[0], at, src_end_index);
        at = at + src_end_index + 1;
        int len = strlen(at);
        at[len - 1] = '\0';
        at[len - 2] = '\0';
        SSB_flash_fs_rename(src_file, at);
        return;
    }

    if (strstr(at, AT_STORAGE_LIST_FILE) != NULL)
    {
        //AT+FLIST=dir\r\n
        if (source == 1)
        {
            SSB_flash_list_file(hand_list_file_call_back);
        }
        return;
    }

    if (strstr(at, AT_STORAGE_REMOVE) != NULL)
    {
        //删除flash里的数据
        //AT+DSMOVE=key\r\n
        at = at + strlen(AT_STORAGE_REMOVE);
        int len = strlen(at);
        at[len - 1] = '\0';
        at[len - 2] = '\0';
        SSB_flash_fs_remove(at);
        return;
    }
#endif
}